package com.sect.edu.info.manager.controller;

import com.sect.edu.info.manager.domain.Student;
import com.sect.edu.info.manager.service.StudentService;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author げっしょく
 * @date 2021/7/17
 */
public class StudentController {
    Scanner sc = new Scanner(System.in);
    StudentService studentService = new StudentService();
    public void exit() {
        Scanner sc = new Scanner(System.in);
        Student[] stu = new Student[10];
        int a = 0;
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            a = sc.nextInt();
            switch (a) {
                case 1:
                    //用户输入你该进入学生管理系统
                    //System.out.println("添加学生");
                    add();
                    break;
                case 2:
                    //System.out.println("删除学生");
                    delete();
                    break;
                case 3:
                    //System.out.println("修改学生");
                    setArrayList();
                    break;
                case 4:
                    //System.out.println("查看学生");
                    AlllookStudent();
                    break;
                case 5:
                    System.out.println("退出学生管理系统");
                    return;
                default:
                    System.out.println("亲！现在还没有该功能哦");
                    break;
            }
        }
    }

    private void AlllookStudent() {
        ArrayList<Student> list = studentService.rtlArray();
        if(list.size() == 0 ){
            System.out.println("您的族谱里面还没有亲人");
            return ;
        }
        System.out.println("学号\t姓名\t\t生日\t\t年龄");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+"\t\t"+stu.getName()+"\t\t"+stu.getBirthday()+"\t\t"+stu.getAge());

        }
    }

    private void setArrayList() {
        System.out.println("请输入你要修改人的学号：");
        String getID = sc.next();
        boolean flag = studentService.isExists(getID);
        if(flag){
            System.out.println("输入修改后的姓名");
            String getname = sc.next();
            System.out.println("输入修改后的生日：");
            String getbirthday = sc.next();
            System.out.println("输入修改后的年龄：");
            int getage = sc.nextInt();
            Student stu = new Student(getID, getname,getbirthday,getage);
            studentService.setData(stu);
            System.out.println("修改成功！！");
        }else{
            System.out.println("没有找到改名学生，您输入的学号有误！！！请重新选择：");
        }

    }

    private void delete() {
        String id ;
        while(true) {
            System.out.println("输入你要删除学生的学号：");
            id = sc.next();
            boolean result = studentService.deleteStuden(id);
            if(result){
                System.out.println("删除成功");
                break;
            }else{
                System.out.println("删除失败，没有该学号");
                break ;
            }
        }
    }

    public void add(){
        String id;
        while (true){
            System.out.println("请输入学生学号：");
            id = sc.next();
            boolean eixt = studentService.isExists(id);
            if(eixt){
                System.out.println("学号已被注册，请重新输入：");
            }else{
                break;
            }
        }
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        Student stu = new Student(id,name,birthday,age);
        boolean result = studentService.addStudent(stu);
        if(result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }
}
